import time

from fastapi.templating import Jinja2Templates
from starlette.responses import JSONResponse

template_response = Jinja2Templates(directory='web/templates').TemplateResponse


def template(request, status_code=200, name="", context=None):
    current_timestamp = time.time()
    current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(current_timestamp))

    if not context:
        context = {}

    return template_response(
        status_code=status_code,
        name=name,
        context={
            'request': request,
            'request_id': request.state.request_id,
            'timestamp': current_timestamp,
            'time': current_time,
            **context
        },

    )


def json_response(request_id, status_code=200, message='success', data=None):
    current_timestamp = time.time()
    current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(current_timestamp))

    return JSONResponse(
        content={
            'status_code': status_code,
            'message': message,
            'data': data,
            'request_id': request_id,
            'timestamp': current_timestamp,
            'time': current_time
        },
        status_code=status_code
    )
